<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js类之属性</title>
</head>
<body>
  <ul>
    <li>类属性大致分为两大类：对象属性和静态属性(属于类，为所有对象共享)</li>
    <li>建议将对象属性都放到constructor中声明和初始化</li>
    <li>可使用static为class定义的类声明静态属性，而function定义的类直接在构造函数上声明静态属性</li>
    <li>可使用static为class定义的类声明静态方法，而function定义的类直接在构造函数上或__proto__声明静态方法</li>
    <li>静态方法可与实例方法同名，但一般不建议这样定义，静态方法中this指向当前类或者构造函数</li>
  </ul>
  <script>
    console.log('****************用class定义类****************')
    {
      class User {
        static web = 'https://github.com/miracle-git'
        constructor(name, age) {
          this.name = name
          this.age = age
        }
        show() {
          console.log(`${this.name} is ${this.age} years old.`)
        }
        static show() {
          console.log(this)
          console.log('static.show in class')
        }
        static create(...args) {
          return new this(...args)
        }
      }
      // const user = new User('Miracle', 35)
      const user = User.create('Miracle', 35)
      user.show()              // Miracle is 35 years old.
      User.show()              // static.show in class
      console.log(User.web)    // https://github.com/miracle-git
      console.dir(User)
      console.dir(user)
    }
    console.log('****************用function定义类****************')
    {
      function User(name, age) {
        this.name = name
        this.age = age
      }
      User.web = 'https://github.com/miracle-git'
      User.__proto__.show = function() {
        console.log(this)
        console.log('static.show in function')
      }
      User.create = function(...args) {
        return new this(...args)
      }
      User.prototype.show = function() {
        console.log(`${this.name} is ${this.age} years old.`)
      }
      // const user = new User('Miracle', 35)
      const user = User.create('Miracle', 35)
      user.show()              // Miracle is 35 years old.
      User.show()              // static.show in function
      console.log(User.web)    // https://github.com/miracle-git
      console.dir(User)
      console.dir(user)
    }
  </script>
</body>
</html>